<template>
  <div>
    <span class="span_item">
      <span class="1" @click="Expanded">
        <el-input style="width: 150px; height: 20px" v-model="node.name" size="small" v-if="node.dep === '4'"
                  maxlength="14"></el-input>
        <span v-if="node.dep !== '4'" style="font-size: 14px">{{node.name}}</span>
      </span>
      <span class="2" v-if="node.dep === '4'">
        <span @click="SaveEdit" size="small">
          <img :src="treeSure" style="vertical-align: middle"/>
        </span>
        <!--<span @click="CancelEdit" size="small">-->
        <!--<img :src="treeCancel" style="vertical-align: middle"/>-->
        <!--</span>-->
      </span>
      <span class="span_icon">
         <span v-if="node.dep === '2'" style="margin-left: 0px" color="gray" type="edit" size="16" @click="redactType(node)">
           <img :src="treeUpdatenormal" style="vertical-align: middle"/>
         </span>
         <span v-if="node.dep === '3'" style="margin-left: 0px" color="gray" type="edit" size="16" @click="redactBrand(node)">
           <img :src="treeUpdatenormal" style="vertical-align: middle"/>
         </span>
         <span v-if="node.dep === '2'" style="margin-left: 0px" type="plus-round" color="red" size="16"
               @click="showAddBrand(node)">
           <img :src="treeAddnormal" style="vertical-align: middle"/>
         </span>
         <span v-if="node.dep === '3'" style="margin-left: 0px" type="plus-round" color="red" size="16"
               @click="showAddProtocol(node)">
           <img :src="treeAddnormal" style="vertical-align: middle"/>
         </span>
         <span v-if="node.dep === '1'" style="margin-left: 0px" type="plus-round" color="red" size="16" @click="showAddType(node)">
           <img :src="treeAddnormal" style="vertical-align: middle"/>
         </span>
         <span v-if="node.dep === '2' " style="margin-left: 0px" type="ios-trash" color="red" size="18" @click="Delete(node)">
           <img :src="treeDeletenormal" style="vertical-align: middle"/>
         </span>
         <span v-if="node.dep === '3'" style="margin-left: 0px" type="ios-trash" color="red" size="18" @click="Delete(node)">
           <img :src="treeDeletenormal" style="vertical-align: middle"/>
         </span>
      </span>
    </span>
    <div class="protocolDialog">
      <div class="addType">
        <el-dialog
          title="新增分类"
          :visible.sync="addTypeDialogShow"
          class="box"
          :before-close="handleClose"
        >
          <el-form :model="typeForm">
            <el-form-item label="ID：" :label-width="formLabelWidth">
              <input v-model="typeForm.resource_id" disabled class="typeInput"/>
            </el-form-item>
            <el-form-item label="分类名称：" :label-width="formLabelWidth">
              <input type="text" maxlength="10" v-model="typeForm.model" class="typeInput" placeholder="请输入分类名称"/>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="confirmAddType()">确 定</el-button>
            <el-button type="info" @click="handleClose()">取 消</el-button>
          </span>
        </el-dialog>
      </div>
      <div class="addBrand">
        <el-dialog
          title="新增品牌"
          :visible.sync="addBrandDialogShow"
          class="box"
          :before-close="handleClose"
        >
          <el-form :model="brandForm">
            <el-form-item label="ID：" :label-width="formLabelWidth">
              <input v-model="brandForm.resource_id" disabled class="typeInput"/>
            </el-form-item>
            <el-form-item label="品牌名称：" :label-width="formLabelWidth">
              <input type="text" maxlength="10" v-model="brandForm.model" class="typeInput" placeholder="请输入品牌名称"/>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="confirmAddBrand()">确 定</el-button>
            <el-button type="info" @click="handleClose()">取 消</el-button>
          </span>
        </el-dialog>
      </div>
      <div class="addProtocol">
        <el-dialog
          title="新增协议库"
          :visible.sync="addProtocolDialogShow"
          class="box"
          :before-close="uploadClose"
        >
          <el-form :model="protocolForm">
            <el-form-item label="ID：" :label-width="formLabelWidth">
              <input v-model="protocolForm.resource_id" disabled class="typeInput"/>
            </el-form-item>
            <el-form-item label="分类：" :label-width="formLabelWidth">
              <input v-model="protocolForm.catagory" disabled class="typeInput"/>
            </el-form-item>
            <el-form-item label="品牌：" :label-width="formLabelWidth">
              <input v-model="protocolForm.brand" disabled class="typeInput"/>
            </el-form-item>
            <el-form-item label="型号：" :label-width="formLabelWidth">
              <input v-model="protocolForm.model" class="typeInput" placeholder="请输入型号"/>
            </el-form-item>
            <el-form-item label="通讯类型：" :label-width="formLabelWidth">
              <el-select v-model="protocolForm.type" placeholder="请选择通讯类型">
                <el-option
                  v-for="item in paramoptions"
                  :key="item.label"
                  :label="item.label"
                  :value="item.label">
                </el-option>
              </el-select>
              <!--<input v-model="protocolForm.type" class="typeInput" placeholder="请输入通讯类型"/>-->
            </el-form-item>
            <el-form-item label="通讯参数：" :label-width="formLabelWidth">
              <input class="typeInput" v-model="protocolForm.param" placeholder="请输入通讯参数"/>
            </el-form-item>
            <el-form-item class="required" label="上传点表：" :label-width="formLabelWidth">
              <template  slot-scope="scope">
                <div class="grid-content bg-purple">
                  <el-upload
                    class="upload-demo"
                    action=""
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :http-request="uploadProtocol"
                    multiple
                    ref="protocolUpload"
                    :limit="1"
                    :on-exceed="handleExceed">
                    <el-button style="color: #52C2D2; height: 25px;width: 80px" size="small">点击上传</el-button>
                    <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
                  </el-upload>
                </div>
              </template>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button :disabled="disableBtn" type="primary" @click="confirmAddProtocol()">确 定</el-button>
            <el-button type="info" @click="uploadClose()">取 消</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
    <div class="redactDialog">
      <div class="redactType">
        <el-dialog
          title="修改分类"
          :visible.sync="redactTypeDialogShow"
          class="box"
          :before-close="handleClose"
        >
          <el-form :model="redactTypeForm">
            <el-form-item label="ID：" :label-width="formLabelWidth">
              <input v-model="redactTypeForm.resource_id" disabled class="typeInput"/>
            </el-form-item>
            <el-form-item label="分类名称：" :label-width="formLabelWidth">
              <input type="text" maxlength="10" v-model="redactTypeForm.model" class="typeInput" placeholder="请输入分类名称"/>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="confirmRedactType()">确 定</el-button>
            <el-button type="info" @click="handleClose()">取 消</el-button>
          </span>
        </el-dialog>
      </div>
      <div class="redactBrand">
        <el-dialog
          title="修改品牌"
          :visible.sync="redactBrandDialogShow"
          class="box"
          :before-close="handleClose"
        >
          <el-form :model="redactBrandForm">
            <el-form-item label="ID：" :label-width="formLabelWidth">
              <input v-model="redactBrandForm.resource_id" disabled class="typeInput"/>
            </el-form-item>
            <el-form-item label="品牌名称：" :label-width="formLabelWidth">
              <input type="text" maxlength="10" v-model="redactBrandForm.model" class="typeInput" placeholder="请输入品牌名称"/>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="confirmRedactBrand()">确 定</el-button>
            <el-button type="info" @click="handleClose()">取 消</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
  import treeDeletenormal from '@/assets/components/treeDeletenormal.png'
  import treeUpdatenormal from '@/assets/components/treeUpdatenormal.png'
  import treeAddnormal from '@/assets/components/treeAddnormal.png'
  import treeCancel from '@/assets/components/treeCancel.png'
  import treeSure from '@/assets/components/treeSure.png'
  import axios from 'axios'
  import treeBus from './bus.js'
  import tableBus from './bus.js'

  export default {
    components: {
    },
    data() {
      return {
        addEquipFalse: false,
        treeDeletenormal,
        treeUpdatenormal,
        treeAddnormal,
        treeCancel,
        treeSure,
        node: this.value,
        projectGid: '',
        nodeData: JSON.parse(JSON.stringify(this.value)),
        addTypeDialogShow: false,
        redactTypeDialogShow: false,
        addBrandDialogShow: false,
        redactBrandDialogShow: false,
        addProtocolDialogShow: false,
        formLabelWidth: '85px',
        disableBtn: true,
        restart: '更新',
        // 下拉框
        paramoptions: [],
        // 图片上传
        fileList: [],
        // typeDialog
        typeForm: {
          resource_id: '',
          model: ''
        },
        // redactTypeDialog
        redactTypeForm: {
          resource_id: '',
          model: ''
        },
        // brandDialog
        brandForm: {
          resource_id: '',
          model: ''
        },
        // redactBrandDialog
        redactBrandForm: {
          resource_id: '',
          model: ''
        },
        // protocolDialog
        protocolForm: {
          resource_id: '',
          model: '',
          param: '',
          type: '',
          brand: '',
          catagory: '',
          attris_file: ''
        },
        // 确认添加分类&品牌
        addTypeBrand: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'projects_type_add',
          data: {
            resource_id: '',
            model: ''
          }
        },
        addProtocol: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'projects_type_add',
          data: {
            resource_id: '',
            model: '',
            param: '',
            type: '',
            brand: '',
            catagory: '',
            attris_file: ''
          }
        },
        // 通讯类型获取
        communityType: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'get_commtype'
        },
        // 新序号获取
        newIdData: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'projects_type_add_genid',
          id: ''
        },
        // 修改Type&Brand
        redactData: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'projects_type_mod',
          data: {
            resource_id: '',
            model: ''
          }
        },
        // 删除
        delSubmitData: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'projects_type_del',
          del: []
        }
      }
    },
    props: {
      value: {
        default: function() {
          return {}
        }
      },
      treeNode: {
        default: function() {
          return {}
        }
      }
    },
    created() {
      this.confirmAlarmdialogShow = true
    },
    mounted() {
      console.log(this.node)
    },
    methods: {
      // 图片上传
      handleRemove(file, fileList) {
        console.log(file, fileList)
        if (fileList.length === 0) {
          this.disableBtn = true
        }
      },
      handlePreview(file) {
        console.log(file)
      },
      handleExceed(files, fileList) {
        // this.$message.warning(`上传失败，当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
        this.$message.warning(`上传失败，当前限制选择 1 个文件。`)
      },
      // 子组件传值
      handleClose() {
        this.addTypeDialogShow = false
        this.addBrandDialogShow = false
        // this.addProtocolDialogShow = false
        this.redactTypeDialogShow = false
        this.redactBrandDialogShow = false
        this.typeForm.resource_id = ''
        this.typeForm.model = ''
        this.brandForm.resource_id = ''
        this.brandForm.model = ''
      },
      uploadClose() {
        this.addProtocolDialogShow = false
        this.protocolForm.resource_id = ''
        this.protocolForm.model = ''
        this.protocolForm.param = ''
        this.protocolForm.type = ''
        this.protocolForm.brand = ''
        this.protocolForm.catagory = ''
        this.protocolForm.attris_file = ''
        this.$refs.protocolUpload.clearFiles()
        this.disableBtn = true
      },
      // 刷新列表
      restar() {
        treeBus.$emit('diaStatus', this.restart)
        console.log('子组件调用')
      },
      // 刷新表格
      restarTable() {
        tableBus.$emit('Table', this.restart)
        console.log('子组件调用')
      },
      reruleStaus: function(dialogStatus) {
        this.restars = dialogStatus
        this.SaveEdit()
      },
      uploadProtocol(item) {
        const formData = new FormData()
        formData.append('file', item.file)
        console.log(item)
        // this.protocolUpload.file = item.file
        // formData.append('group', 'system')
        axios.post('/protocol/upload', formData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
          .then((res) => {
            this.protocolForm.attris_file = res.data.data.file_name
            console.log(res)
            this.disableBtn = false
          })
          .catch((error) => {
            this.$message({
              type: 'error',
              message: '请上传 .spl 格式文件'
            })
            this.disableBtn = true
            console.log(error)
          })
      },
      // 下拉获取
      typeSelect() {
        axios.post('/protocol', this.communityType, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.paramoptions = res.data.data
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
      },
      // 弹窗
      showAddType(val) {
        this.addTypeDialogShow = true
        console.log(11111)
        console.log(val)
        this.newIdData.id = val.resource_id
        axios.post('/protocol', this.newIdData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.typeForm.resource_id = res.data.data.id
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
      },
      showAddBrand(val) {
        this.addBrandDialogShow = true
        console.log(22222)
        console.log(val)
        this.newIdData.id = val.resource_id
        axios.post('/protocol', this.newIdData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.brandForm.resource_id = res.data.data.id
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
      },
      showAddProtocol(val) {
        this.typeSelect()
        this.addProtocolDialogShow = true
        console.log(33333)
        console.log(val)
        this.newIdData.id = val.resource_id
        axios.post('/protocol', this.newIdData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.protocolForm.resource_id = res.data.data.id
          this.protocolForm.brand = res.data.data.brand
          this.protocolForm.catagory = res.data.data.type
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
      },
      // 确认添加
      confirmAddType() {
        this.addTypeBrand.data = this.typeForm
        axios.post('/protocol', this.addTypeBrand, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.restar()
            this.typeForm.resource_id = ''
            this.typeForm.model = ''
          }
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
          //        this.handleClose()
          //        this.givereload()
      },
      confirmAddBrand() {
        this.addTypeBrand.data = this.brandForm
        axios.post('/protocol', this.addTypeBrand, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.restar()
            this.brandForm.resource_id = ''
            this.brandForm.model = ''
          }
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
        // this.handleClose()
      },
      confirmAddProtocol() {
        this.addProtocol.data = this.protocolForm
        axios.post('/protocol', this.addProtocol, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.restarTable()
            this.uploadClose()
          }
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
        // this.uploadClose()
      },
      // 修改弹窗
      redactType(val) {
        this.redactTypeDialogShow = true
        console.log(val)
        this.redactTypeForm.resource_id = val.resource_id
        this.redactTypeForm.model = val.name
      },
      redactBrand(val) {
        this.redactBrandDialogShow = true
        console.log(val)
        this.redactBrandForm.resource_id = val.resource_id
        this.redactBrandForm.model = val.name
      },
      confirmRedactType() {
        this.redactData.data = this.redactTypeForm
        axios.post('/protocol', this.redactData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.restar()
          }
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
        this.handleClose()
      },
      confirmRedactBrand() {
        this.redactData.data = this.redactBrandForm
        axios.post('/protocol', this.redactData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.restar()
          }
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
          })
        this.handleClose()
      },
      Delete(rows) {
        this.$emit('Delete', this.nodeData)
        console.log(rows)
        var idstr = rows.resource_id
        this.delSubmitData.del.push(idstr)
        console.log(this.delSubmitData.del)
        this.$confirm('确认删除协议库？', '提示', {
          cancelButtonClass: 'userCancel',
          confirmButtonClass: 'userConfirm',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log()
          axios.post('/protocol', this.delSubmitData, {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            }
          }).then((res) => {
            console.log(res)
            if (res.data.errcode === '00000') {
              this.restar()
            }
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
            .catch((error) => {
              console.log(error)
              this.$message({
                type: 'error',
                message: '删除失败, 可能有子目录或子协议存在'
              })
            })
        }).catch(() => {
        })
      },
      Expanded() {
        this.treeNode.expanded = !this.treeNode.expanded
      }
    },
    watch: {
      value: {
        handler: function(val, oldVal) {
          this.node = val
        },
        deep: true
      },
      node: {
        handler: function(val) {
          this.$emit('input', val)
        },
        deep: true
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-button--small {
    padding: 1px 4px;
    font-size: 12px;
    border-radius: 3px;
  }

  /deep/ .el-input--small .el-input__inner {
    height: 23px;
    line-height: 18px;
  }

  .span_item:hover .span_icon {
    display: inline;
  }

  .span_icon {
    margin-left: 10px;
    display: none;
  }
  /*****************第一层弹窗*****************/
  /deep/ .box > .el-dialog{
    width: 540px !important;
    min-height: 200px;
    margin-top: 30vh !important;
    border-radius: 4px;
  }
  /*****************输入框*****************/
  .typeInput{
    width: 425px;
    height: 25px;
    border-radius: 4px;
    border: 1px solid #ccc;
    outline: none;
    text-indent: 10px;
  }
  input::-webkit-input-placeholder{
    color: #C0C4CC;
  }
  /************下拉框样式************/
  /deep/ .el-input__inner{
    height: 25px;
    width: 425px;
    outline: none;
  }
  /*****************第一层弹窗头部*****************/
  /deep/ .box > .el-dialog > .el-dialog__header{
    width: 540px;
    height: 39px;
    line-height: 43px;
    padding: 0;
    text-indent: 20px;
    font-weight: bolder;
    border-radius: 4px 4px 0 0;
    background-color: #e3e6ee;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn{
    top: 12px;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn > i{
    font-size: 18px;
    font-weight: bolder;
  }
  /*****************第一层弹窗内容*****************/
  /deep/ .box > .el-dialog > .el-dialog__body{
    padding: 0 0 0 10px ;
    margin-top: 20px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item{
    margin-bottom: 0px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__label{
    color: #69727E;
    padding: 0;
    font-weight: normal;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__content > .el-input > .el-input__inner{
    width: 240px;
    height: 30px;
    padding: 0 5px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .borderNone > .el-form-item__content > .el-input > .el-input__inner{
    border: none;
  }
  /*****************第一层弹窗底部*****************/
  /deep/ .box > .el-dialog > .el-dialog__footer{
    border-top: 1px solid #e3e6ee;
  }
  /*****************底部按钮*****************/
  /deep/ .dialog-footer{
    padding-top: 8px;
  }
  /deep/ .dialog-footer > .el-button{
    width: 80px;
    height: 30px;
    padding: 0;
  }
  /**************required**************/
  /deep/ .required > label:before{
    display: inline-block;
    content: "*";
    color:  #DE6669;
  }
</style>
